﻿namespace Yaaf.Wp7.AdvancedExplorer.ViewModels
{
    using System;
    using System.ComponentModel;
    using System.IO;
    using System.Windows.Media.Imaging;

    using Yaaf.Wp7.AdvancedExplorer.Resources;
    using Yaaf.WP7.NativeAccess.IO;
    using Yaaf.WP7.NativeAccess.Provision;

    public class FileEntryViewModel : INotifyPropertyChanged
    {
        private readonly FileSystemEntry fileSystemEntry;

        public FileEntryViewModel(FileSystemEntry fileSystemEntry)
        {
            this.fileSystemEntry = fileSystemEntry;
            this.elementName = fileSystemEntry.Name;
            var file = fileSystemEntry as WP7.NativeAccess.IO.FileInfo;
            if (file != null)
            {
                this.Size = file.Size;
            }
        }

        public FileEntryViewModel()
        {
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private bool isSelected;

        private string elementName;

        public BitmapImage Icon
        {
            get
            {
                return IconStore.GetImage(fileSystemEntry);
            }
        }

        public long Size { get; private set; }

        private string GetSizeInternal()
        {
            double c = Size;
            double num2 = 1024.0;
            if (c < num2)
            {
                return (c + " " + AppResources.BytesShort);
            }
            c /= num2;
            if (c < num2)
            {
                return (Math.Round(c, 2) + " " + AppResources.KilobytesShort);
            }
            c /= num2;
            return (Math.Round(c, 2) + " " + AppResources.MegabytesShort);
        }

        public string SizeString
        {
            get
            {
                var file = fileSystemEntry as WP7.NativeAccess.IO.FileInfo;
                if (file == null)
                {
                    return string.Empty;
                }

                return "(" + this.GetSizeInternal() + ")";
            }
        }

        public bool IsSelected
        {
            get
            {
                return this.isSelected;
            }
            set
            {
                this.isSelected = value;
                this.NotifyPropertyChanged("IsSelected");
            }
        }

        public string ElementName
        {
            get
            {
                return this.elementName;
            }
            set
            {
                this.elementName = value;
            }
        }

        public FileSystemEntry Element
        {
            get
            {
                return fileSystemEntry;
            }
        }

        private bool editMode;

        public bool EditMode
        {
            get
            {
                return this.editMode;
            }
            set
            {
                this.editMode = value;
                this.NotifyPropertyChanged("EditMode");
                this.NotifyPropertyChanged("NEditMode");
            }
        }

        public bool NEditMode
        {
            get
            {
                return !this.EditMode;
            }
            set
            {
                this.EditMode = !value;
            }
        }

        public void Upload()
        {
            App.ViewModel.ExplorerView.AddTasks(Element);
        }

        public void ProvisionIfPossible()
        {
            var file = Element as WP7.NativeAccess.IO.FileInfo;
            if (file == null)
            {
                return;
            }

            using (var reader = new StreamReader(file.Open(FileMode.Open, FileAccess.Read)))
            {
                var all = reader.ReadToEnd();
                ProvisionXml.Provision(all);
            }
        }
    }
}